<?php
/**
 * Pertyler DatabaseManagement
 * Control
 * SearchedUnpublishedRowArray
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once __DIR__ . '/main.php';

if(isset($_POST['Update']))
	return require __DIR__ . '/UpdateRow.php';

if(!isset($_GET['Table']))
	throw new \Exception('Table parameter required.');

require_once __DIR__ . '/../databaseModel/Role.php';

if(!($moderator = Role_Moderator::Select()))
	return require __DIR__ . '/Index.php';

require_once __DIR__ . '/../databaseModel/Table.php';

if(!($table = Table::Select($_GET['Table'])))
	throw new \Exception('Table cannot found.');

if(!$table->SelectPrivilege('select'))
	throw new \Exception('Table does not have select privilege.');

if(!Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select')))
	throw new \Exception('Select privilege required.');

if(!$table->HasUnpublishedColumn())
	throw new \Exception('Table does not have unpublished column.');

require_once __DIR__ . '/../library/Pert/General/Page.php';

\Pert\Page::Get()->AddTitle($table);

$Content = \Pert\Page::Get()->Content();

$Content->TableName = (string) $table;

$Content->TableId = $table->Id();

$stringColumnArray = array();

$stringColumnValueArray = array();

$referancedColumnArray = array();

$referancedColumnTableIdArray = array();

$referancedColumnValueArrayArray = array();

foreach($table->SelectColumnArray() AS $column)
	if($column->String())
	{
		if(isset($_POST[$column->Priority()]))
			if($_POST[$column->Priority()])
			{
				$stringColumnArray[$column->Priority()] = (string) $column;

				$stringColumnValueArray[$column->Priority()] = $_POST[$column->Priority()];

				$columnValueArray[$column->Priority()] = new Row_ColumnValue($column,
						$stringColumnValueArray[$column->Priority()]);
			}
	}
	elseif($column->RelatedTable())
		if(isset($_POST[$column->Priority()]))
			if($_POST[$column->Priority()])
			{
				$referancedColumnArray[$column->Priority()] = (string) $column;

				$referancedColumnTableIdArray[$column->Priority()] = $column->RelatedTable()->Id();

				$referancedColumnValueArrayArray[$column->Priority()] = $column->RelatedTable()->SelectRowArray($_POST[$column->Priority()]);

				$columnValueArray[$column->Priority()] = new Row_ColumnValue($column,
						$referancedColumnValueArrayArray[$column->Priority()]);
			}

$Content->StringColumnArray = $stringColumnArray;

unset($stringColumnArray);

$Content->StringColumnValueArray = $stringColumnValueArray;

unset($stringColumnValueArray);

$Content->ReferancedColumnArray = $referancedColumnArray;

unset($referancedColumnArray);

$Content->ReferancedColumnTableIdArray = $referancedColumnTableIdArray;

unset($referancedColumnTableIdArray);

$Content->ReferancedColumnValueArrayArray = $referancedColumnValueArrayArray;

unset($referancedColumnValueArrayArray);

$unpublishedRowArray = $table->SelectUnpublishedRowArray();

$unpublishedRowColumnValueArrayArray = array();

foreach($unpublishedRowArray AS $unpublishedRow)
	$unpublishedRowColumnValueArrayArray[$unpublishedRow->Id()][] = (string) $unpublishedRow;

unset($unpublishedRowArray);

unset($unpublishedRow);

$Content->UnpublishedRowColumnValueArrayArray = $unpublishedRowColumnValueArrayArray;

unset($unpublishedRowColumnValueArrayArray);

if($table->SelectPrivilege('update'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('update')))
	{
		$Content->UpdatePrivilege = true;

		$Content->UpdateAsPublishedPrivilege = true;
	}

if($table->SelectPrivilege('approve'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('approve')))
		$Content->ApprovePrivilege = true;

if($table->SelectPrivilege('delete'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('delete')))
		$Content->DeleteUnpublishedPrivilege = true;

unset($table);

unset($_GET);

unset($_POST);

echo \Pert\Page::Get()->HeaderHTML();

require __DIR__ . '/../view/SearchedRowArray.php';

require __DIR__ . '/../view/UnpublishedRowArrayGrid.php';

echo \Pert\Page::Get()->FooterHTML();

return true;
